package com.xxwy.bean.instantiation;

import com.xxwy.bean.DefaultUserFactory;
import com.xxwy.bean.User;
import com.xxwy.bean.UserFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * @author wy
 * @date 2020/4/21 14:51
 */
public class SpecialBeanInstantiation {
  public static void main(String[] args) {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:/META-INF/bean-specialcreate-context.xml");
    ServiceLoader<UserFactory> serviceLoader = applicationContext.getBean("userFactoryServiceLoader", ServiceLoader.class);
    serviceLoader(serviceLoader);

    AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
    DefaultUserFactory defaultUserFactory = beanFactory.createBean(DefaultUserFactory.class);
    User user = beanFactory.createBean(User.class);

  }

  public static void serviceLoader(ServiceLoader<UserFactory> serviceLoader){
    //ServiceLoader<UserFactory> serviceLoader = ServiceLoader.load(UserFactory.class, Thread.currentThread().getContextClassLoader());
    Iterator<UserFactory> iterator = serviceLoader.iterator();
    while (iterator.hasNext()){
      UserFactory userFactory = iterator.next();
      System.out.println(userFactory.getUser());
    }
  }


}
